/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.great.functionalCore;

import java.io.FileOutputStream;
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.beans.*;
import br.great.config.GetCheminServer;

/**
 *
 * @author C�line DEFAUT
 */
public class NewOntologyCollectionCreator {
    // create a new file containing the annotations (captured and modified) of a collection
//    private static String serverfileAddress="D:\\Documents and Settings\\Administrator.3DF5CEE6F12E44B\\My Documents\\NetBeansProjects\\PhotoMapServer\\PhotoMapBeta_Web\\web\\";
    
    private FileOutputStream newOnto;
    private Collection coll;
    
    /** Creates a new instance of NewOntologyCollectionCreator_parser */
    public NewOntologyCollectionCreator() {
    }
    
    public File write(Collection c, String loginUser) {
        String pathOntoPhoto = GetCheminServer.serverfileAddress+"photos\\"+loginUser+"\\"+c.getName()+"\\"+c.getName()+".owl";
        try {
//            System.out.println("d�but write collection");
            coll = c;
            File file = new File(pathOntoPhoto);
            newOnto = new FileOutputStream(file);

            // en-t�te xml
            String s = "<?xml version=\"1.0\"?>\n";

            // espace de noms
            s += "<rdf:RDF xmlns:ctxtphoto=\"http://www-lsr.imag.fr/users/Windson.Viana/ontologies/ContextPhoto.owl#\""+
                    "\nxmlns:time=\"http://www.w3.org/2006/time#\""+
                    "\nxmlns:protege=\"http://protege.stanford.edu/plugins/owl/protege#\""+
                    "\nxmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""+
                    "\nxmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\" " +
                    "\nxmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\""+
                    "\nxmlns:owl=\"http://www.w3.org/2002/07/owl#\" " +
                    "\nxmlns:gml=\"http://www.opengis.net/gml/\"" +
                    "\nxmlns:georss=\"http://www.georss.org/georss/\"" +

                     "\n";
            s += "xmlns=\"" + coll.getAbout() + "#\" \n";
            Iterator it1 = coll.getPhotos().iterator();
            int i = 1; // number of the photo
            while (it1.hasNext()) {
                Photo p = (Photo) it1.next();
                s += "xmlns:photo" + i + "=\"" + p.getAbout() + "#\" \n";
                i++;
            }
            s += "xml:base=\"" + coll.getAbout() + "\"";
            s += ">\n";

            // imports
            s += "<owl:Ontology rdf:about=\"\">\n";
            s += "<owl:imports rdf:resource=\"http://www-lsr.imag.fr/users/Windson.Viana/ontologies/ContextPhoto.owl\"/>\n";
            Iterator it2 = coll.getPhotos().iterator();
            while (it2.hasNext()) {
                Photo p = (Photo) it2.next();
                s += "<owl:imports rdf:resource=\"" + getURL(p.getAbout()) + "\"/> \n";
            }
            s += "</owl:Ontology>\n";

            
            if (coll.getTrackPointList() != null) {
                s += "<ctxtphoto:TrackList rdf:ID=\"TrackList\">\n";
                Iterator it = coll.getTrackPointList().iterator();
                int j = 1;
                while (it.hasNext()) {
                    TrackPoint tp = (TrackPoint) it.next();
                    s += "<ctxtphoto:hasTrackPoint>\n";
                    s += "<ctxtphoto:TrackPoint rdf:ID=\"TrackPoint_" + j + "\">\n";
                    s += "<j.0:point rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + tp.getLatitude() + " " + tp.getLongitude() + "</j.0:point>\n";
                    Date d = tp.getDateTime();
                    if (d != null) {
                        s += "<time:inXSDDateTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">" + d.getYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "</time:inXSDDateTime>\n";
                    }
                    s += "</ctxtphoto:TrackPoint>\n</ctxtphoto:hasTrackPoint>\n";
                    j++;
                }
                s += "</ctxtphoto:TrackList>\n";
            }


            // Event_Collection
            s += "<ctxtphoto:Event_Collection rdf:ID=\"" + coll.getName() + "\">\n";

            // track list
            if (coll.getTrackPointList() != null) {
                s += "<ctxtphoto:hasTrackList rdf:resource=\"#TrackList\"/>\n";
            }
            // tags
            Vector<Tag> tags = coll.getTags();
            if (tags != null) {
                Iterator itTags = tags.iterator();
                int nTag = 1;
                while (itTags.hasNext()) {
                    Tag tag = (Tag) itTags.next();
                    s += "<ctxtphoto:content_annotation> \n<ctxtphoto:Tag rdf:ID=\"tag" + nTag + "\"> \n<ctxtphoto:tag rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + tag.getTAG_VALUE() + "</ctxtphoto:tag> \n</ctxtphoto:Tag> \n</ctxtphoto:content_annotation>\n";
                    nTag++;
                }
            }

            // textual_description
            String desc = coll.getContentAnnotation();
            if (desc != null) {
                s += "<ctxtphoto:content_annotation> \n<ctxtphoto:Textual_Description rdf:ID=\"collection_description\">\n<ctxtphoto:textual_description xml:lang=\"en\">" + desc + "</ctxtphoto:textual_description>\n</ctxtphoto:Textual_Description>\n</ctxtphoto:content_annotation>\n";
            }
            //time begin
            Date b = coll.getBegin();
            if (b != null) {
                s += "<time:hasEnd> \n<time:Instant rdf:ID=\"collectionInstantEnd\">\n";
                String month = (b.getMonth() + 1) + "";
                if (month.length() < 2) {
                    String month2 = "0" + month;
                    month = month2;
                }
                String day = b.getDate() + "";
                if (day.length() < 2) {
                    String day2 = "0" + day;
                    day = day2;
                }
                String hours = b.getHours() + "";
                if (hours.length() < 2) {
                    String hours2 = "0" + hours;
                    hours = hours2;
                }
                String minutes = b.getMinutes() + "";
                if (minutes.length() < 2) {
                    String minutes2 = "0" + minutes;
                    minutes = minutes2;
                }
                String seconds = b.getHours() + "";
                if (seconds.length() < 2) {
                    String seconds2 = "0" + seconds;
                    seconds = seconds2;
                }
                s += "<time:hasDurationDescription> \n<time:DurationDescription rdf:ID=\"DurationDescriptionEnd\"> \n<time:hours rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + hours + "</time:hours> \n<time:years rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + b.getYear() + "</time:years> \n<time:months rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + month + "</time:months>\n<time:days rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + day + "</time:days> \n <time:minutes rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + minutes + "</time:minutes> \n<time:seconds rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + seconds + "</time:seconds> \n</time:DurationDescription>\n</time:hasDurationDescription>";
                String dateB = b.getYear() + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
                s += "<time:inXSDDateTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">" + dateB + "</time:inXSDDateTime>\n";
                s += "</time:Instant>\n</time:hasEnd>\n";
            }

            // time end
            Date e = coll.getEnd();
            if (e != null) {
                s += "<time:hasEnd> \n<time:Instant rdf:ID=\"collectionInstantEnd\">\n";
                String month = (e.getMonth() + 1) + "";
                if (month.length() < 2) {
                    String month2 = "0" + month;
                    month = month2;
                }
                String day = e.getDate() + "";
                if (day.length() < 2) {
                    String day2 = "0" + day;
                    day = day2;
                }
                String hours = e.getHours() + "";
                if (hours.length() < 2) {
                    String hours2 = "0" + hours;
                    hours = hours2;
                }
                String minutes = e.getMinutes() + "";
                if (minutes.length() < 2) {
                    String minutes2 = "0" + minutes;
                    minutes = minutes2;
                }
                String seconds = e.getHours() + "";
                if (seconds.length() < 2) {
                    String seconds2 = "0" + seconds;
                    seconds = seconds2;
                }
                s += "<time:hasDurationDescription> \n<time:DurationDescription rdf:ID=\"DurationDescriptionEnd\"> \n<time:hours rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + hours + "</time:hours> \n<time:years rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + e.getYear() + "</time:years> \n<time:months rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + month + "</time:months>\n<time:days rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + day + "</time:days> \n <time:minutes rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + minutes + "</time:minutes> \n<time:seconds rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + seconds + "</time:seconds> \n</time:DurationDescription>\n/time:hasDurationDescription>";
                String dateE = e.getYear() + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
                s += "<time:inXSDDateTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">" + dateE + "</time:inXSDDateTime>\n";
                s += "</time:Instant>\n</time:hasEnd>\n";
            }

            //photos
            Iterator itP = coll.getPhotos().iterator();
            while (itP.hasNext()) {
                Photo p = (Photo) itP.next();
                s += "<ctxtphoto:photo> \n<rdf:Description rdf:about=\"" + p.getAbout() + "\"> \n<ctxtphoto:isPartOf rdf:resource=\"#" + coll.getName() + "\"/> \n</rdf:Description>\n</ctxtphoto:photo>";
            }
            s += "</ctxtphoto:Event_Collection>\n";

            s += "</rdf:RDF>";

//            System.out.println(s);
            byte[] annot = s.getBytes();
            newOnto.write(annot);
            return file;
        } catch (Exception ex) {
            Logger.getLogger(NewOntologyCollectionCreator.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    /**
     * parameter about
     *          example : http://www-lsr.imag.fr/users/Windson.Viana/ontologies/romanForum1.owl#romanForum1
     *return 
     *          http://www-lsr.imag.fr/users/Windson.Viana/ontologies/romanForum1.owl
     */
    private String getURL(String about){
        String result = "";
        int indexFin = about.indexOf('#');
        if (indexFin>=0){
            result = about.substring(0, indexFin);
        }
        return result;
    }
    
//    public static void main (String[] args){
//        try {
//            Collection collection = new Collection();
//            //coll.setURI("P:\\Bureau\\projet_PHOTOMAP\\CONTEXT_PHOTO\\fichiersOwlMobile\\RomanForumCollectionv2.owl");
//            collection.setURI("C:\\Users\\C�line DEFAUT\\Desktop\\projet_PHOTOMAP\\CONTEXT_PHOTO\\fichiersOwlMobile\\RomanForumCollectionv2.owl");
//            collection.setAbout("http://www-lsr.imag.fr/users/Windson.Viana/ontologies/romanForumCollectionv2.owl#romanForum");
//            collection.setName("RomanForumCollectionv2");
//            collection.setContentAnnotation("other textual description");
//            Vector<String> tags = new Vector<String>();
//            tags.add("autreTag");
//            collection.setTags(tags);
//            Date b = new Date(2008, 01, 10, 15, 01, 04);
//            collection.setBegin(b);
//            Date e = new Date(2008, 01, 10, 15, 22, 41);
//            collection.setEnd(e);
//            Photo p = new Photo();
//            p.setAbout("http://www-lsr.imag.fr/users/Windson.Viana/ontologies/romanForum1.owl#romanForum1");
//            Vector<Photo> photos = new Vector<Photo>();
//            photos.add(p);
//            collection.setPhotos(photos);
//            NewOntologyCollectionCreator.write(collection);
//
//            
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//    }

    
}
